home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / p_man / cat3 / Math::Trig.Z / Math::Trig
Encoding:
Text File  |  1998-10-28  |  14.4 KB  |  397 lines

  1.  
  2.  
  3.  
  4.      MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))   22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))     MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       Math::Trig - trigonometric functions
  10.  
  11.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.           use Math::Trig;
  13.  
  14.           $x = tan(0.9);
  15.           $y = acos(3.7);
  16.           $z = asin(2.4);
  17.  
  18.           $halfpi = pi/2;
  19.  
  20.           $rad = deg2rad(120);
  21.  
  22.  
  23.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  24.       Math::Trig defines many trigonometric    functions not defined
  25.       by the core Perl which defines only the sin()    and cos().
  26.       The constant ppppiiii is also defined as are a few convenience
  27.       functions for    angle conversions.
  28.  
  29.      TTTTRRRRIIIIGGGGOOOONNNNOOOOMMMMEEEETTTTRRRRIIIICCCC FFFFUUUUNNNNCCCCTTTTIIIIOOOONNNNSSSS
  30.       The tangent
  31.  
  32.       ttttaaaannnn
  33.  
  34.       The cofunctions of the sine, cosine, and tangent (cosec/csc
  35.       and cotan/cot    are aliases)
  36.  
  37.       ccccsssscccc, ccccoooosssseeeecccc, sssseeeecccc, sssseeeecccc,    ccccooootttt, ccccoooottttaaaannnn
  38.  
  39.       The arcus (also known    as the inverse)    functions of the sine,
  40.       cosine, and tangent
  41.  
  42.       aaaassssiiiinnnn,    aaaaccccoooossss, aaaattttaaaannnn
  43.  
  44.       The principal    value of the arc tangent of y/x
  45.  
  46.       aaaattttaaaannnn2222(y, x)
  47.  
  48.       The arcus cofunctions    of the sine, cosine, and tangent
  49.       (acosec/acsc and acotan/acot are aliases)
  50.  
  51.       aaaaccccsssscccc,    aaaaccccoooosssseeeecccc,    aaaasssseeeecccc, aaaaccccooootttt, aaaaccccoooottttaaaannnn
  52.  
  53.       The hyperbolic sine, cosine, and tangent
  54.  
  55.       ssssiiiinnnnhhhh,    ccccoooosssshhhh, ttttaaaannnnhhhh
  56.  
  57.       The cofunctions of the hyperbolic sine, cosine, and tangent
  58.       (cosech/csch and cotanh/coth are aliases)
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))   22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))     MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))
  71.  
  72.  
  73.  
  74.       ccccsssscccchhhh,    ccccoooosssseeeecccchhhh,    sssseeeecccchhhh, ccccooootttthhhh, ccccoooottttaaaannnnhhhh
  75.  
  76.       The arcus (also known    as the inverse)    functions of the
  77.       hyperbolic sine, cosine, and tangent
  78.  
  79.       aaaassssiiiinnnnhhhh, aaaaccccoooosssshhhh,    aaaattttaaaannnnhhhh
  80.  
  81.       The arcus cofunctions    of the hyperbolic sine,    cosine,    and
  82.       tangent (acsch/acosech and acoth/acotanh are aliases)
  83.  
  84.       aaaaccccsssscccchhhh, aaaaccccoooosssseeeecccchhhh, aaaasssseeeecccchhhh, aaaaccccooootttthhhh,    aaaaccccoooottttaaaannnnhhhh
  85.  
  86.       The trigonometric constant ppppiiii    is also    defined.
  87.  
  88.       $pi2 = 2 * ppppiiii;
  89.  
  90.       EEEERRRRRRRROOOORRRRSSSS DDDDUUUUEEEE TTTTOOOO    DDDDIIIIVVVVIIIISSSSIIIIOOOONNNN BBBBYYYY ZZZZEEEERRRROOOO
  91.  
  92.       The following    functions
  93.  
  94.           acoth
  95.           acsc
  96.           acsch
  97.           asec
  98.           asech
  99.           atanh
  100.           cot
  101.           coth
  102.           csc
  103.           csch
  104.           sec
  105.           sech
  106.           tan
  107.           tanh
  108.  
  109.       cannot be computed for all arguments because that would mean
  110.       dividing by zero or taking logarithm of zero.    These
  111.       situations cause fatal runtime errors    looking    like this
  112.  
  113.           cot(0): Division by zero.
  114.           (Because in the definition of    cot(0),    the divisor sin(0) is 0)
  115.           Died at ...
  116.  
  117.       or
  118.  
  119.           atanh(-1): Logarithm of zero.
  120.           Died at...
  121.  
  122.       For the csc, cot, asec, acsc,    acot, csch, coth, asech,
  123.       acsch, the argument cannot be    0 (zero).  For the atanh,
  124.       acoth, the argument cannot be    1 (one).  For the atanh,
  125.       acoth, the argument cannot be    -1 (minus one).     For the tan,
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))   22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))     MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))
  137.  
  138.  
  139.  
  140.       sec, tanh, sech, the argument    cannot be _p_i/_2 + _k * _p_i, where
  141.       _k is any integer.
  142.  
  143.       SSSSIIIIMMMMPPPPLLLLEEEE ((((RRRREEEEAAAALLLL))))    AAAARRRRGGGGUUUUMMMMEEEENNNNTTTTSSSS,,,, CCCCOOOOMMMMPPPPLLLLEEEEXXXX RRRREEEESSSSUUUULLLLTTTTSSSS
  144.  
  145.       Please note that some    of the trigonometric functions can
  146.       break    out from the rrrreeeeaaaallll aaaaxxxxiiiissss into the    ccccoooommmmpppplllleeeexxxx    ppppllllaaaannnneeee. For
  147.       example asin(2) has no definition for    plain real numbers but
  148.       it has definition for    complex    numbers.
  149.  
  150.       In Perl terms    this means that    supplying the usual Perl
  151.       numbers (also    known as scalars, please see the _p_e_r_l_d_a_t_a
  152.       manpage) as input for    the trigonometric functions might
  153.       produce as output results that no more are simple real
  154.       numbers: instead they    are complex numbers.
  155.  
  156.       The Math::Trig handles this by using the Math::Complex
  157.       package which    knows how to handle complex numbers, please
  158.       see the _M_a_t_h::_C_o_m_p_l_e_x    manpage    for more information. In
  159.       practice you need not    to worry about getting complex numbers
  160.       as results because the Math::Complex takes care of details
  161.       like for example how to display complex numbers. For
  162.       example:
  163.  
  164.           print    asin(2), "\n";
  165.  
  166.       should produce something like    this (take or leave few    last decimals):
  167.  
  168.           1.5707963267949-1.31695789692482i
  169.  
  170.       That is, a complex number with the real part of
  171.       approximately    1.571 and the imaginary    part of    approximately
  172.       -1.317.
  173.  
  174.      PPPPLLLLAAAANNNNEEEE AAAANNNNGGGGLLLLEEEE CCCCOOOONNNNVVVVEEEERRRRSSSSIIIIOOOONNNNSSSS
  175.       (Plane, 2-dimensional) angles    may be converted with the
  176.       following functions.
  177.  
  178.           $radians  = deg2rad($degrees);
  179.           $radians  = grad2rad($gradians);
  180.  
  181.           $degrees  = rad2deg($radians);
  182.           $degrees  = grad2deg($gradians);
  183.  
  184.           $gradians = deg2grad($degrees);
  185.           $gradians = rad2grad($radians);
  186.  
  187.       The full circle is 2 _p_i radians or _3_6_0 degrees or _4_0_0
  188.       gradians.
  189.  
  190.      RRRRAAAADDDDIIIIAAAALLLL CCCCOOOOOOOORRRRDDDDIIIINNNNAAAATTTTEEEE CCCCOOOONNNNVVVVEEEERRRRSSSSIIIIOOOONNNNSSSS
  191.       RRRRaaaaddddiiiiaaaallll ccccoooooooorrrrddddiiiinnnnaaaatttteeee ssssyyyysssstttteeeemmmmssss are    the sssspppphhhheeeerrrriiiiccccaaaallll and the
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))   22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))     MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))
  203.  
  204.  
  205.  
  206.       ccccyyyylllliiiinnnnddddrrrriiiiccccaaaallll systems, explained shortly in more detail.
  207.  
  208.       You can import radial    coordinate conversion functions    by
  209.       using    the :radial tag:
  210.  
  211.           use Math::Trig ':radial';
  212.  
  213.           ($rho, $theta, $z)     = cartesian_to_cylindrical($x, $y,    $z);
  214.           ($rho, $theta, $phi)   = cartesian_to_spherical($x, $y, $z);
  215.           ($x, $y, $z)         = cylindrical_to_cartesian($rho, $theta, $z);
  216.           ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c,    $theta,    $z);
  217.           ($x, $y, $z)         = spherical_to_cartesian($rho, $theta, $phi);
  218.           ($rho_c, $theta, $z)   = spherical_to_cylindrical($rho_s,    $theta,    $phi);
  219.  
  220.       AAAAllllllll aaaannnngggglllleeeessss aaaarrrreeee iiiinnnn rrrraaaaddddiiiiaaaannnnssss.
  221.  
  222.       CCCCOOOOOOOORRRRDDDDIIIINNNNAAAATTTTEEEE SSSSYYYYSSSSTTTTEEEEMMMMSSSS
  223.  
  224.       CCCCaaaarrrrtttteeeessssiiiiaaaannnn coordinates    are the    usual rectangular (_x, _y,
  225.       _z)-coordinates.
  226.  
  227.       Spherical coordinates, (_r_h_o, _t_h_e_t_a, _p_i), are three-
  228.       dimensional coordinates which    define a point in three-
  229.       dimensional space.  They are based on    a sphere surface.  The
  230.       radius of the    sphere is rrrrhhhhoooo, also known as the _r_a_d_i_a_l
  231.       coordinate.  The angle in the    _x_y-plane (around the _z-axis)
  232.       is tttthhhheeeettttaaaa, also known as the _a_z_i_m_u_t_h_a_l    coordinate.  The angle
  233.       from the _z-axis is pppphhhhiiii, also known as    the _p_o_l_a_r coordinate.
  234.       The `North Pole' is therefore    _0, _0, _r_h_o, and the `Bay    of
  235.       Guinea' (think of the    missing    big chunk of Africa) _0,    _p_i/_2,
  236.       _r_h_o.
  237.  
  238.       BBBBeeeewwwwaaaarrrreeee: some texts define _t_h_e_t_a and _p_h_i the other way    round,
  239.       some texts define the    _p_h_i to start from the horizontal
  240.       plane, some texts use    _r in place of _r_h_o.
  241.  
  242.       Cylindrical coordinates, (_r_h_o, _t_h_e_t_a,    _z), are    three-
  243.       dimensional coordinates which    define a point in three-
  244.       dimensional space.  They are based on    a cylinder surface.
  245.       The radius of    the cylinder is    rrrrhhhhoooo, also known    as the _r_a_d_i_a_l
  246.       coordinate.  The angle in the    _x_y-plane (around the _z-axis)
  247.       is tttthhhheeeettttaaaa, also known as the _a_z_i_m_u_t_h_a_l    coordinate.  The third
  248.       coordinate is    the _z, pointing    up from    the tttthhhheeeettttaaaa-plane.
  249.  
  250.       3333----DDDD AAAANNNNGGGGLLLLEEEE CCCCOOOONNNNVVVVEEEERRRRSSSSIIIIOOOONNNNSSSS
  251.  
  252.       Conversions to and from spherical and    cylindrical
  253.       coordinates are available.  Please notice that the
  254.       conversions are not necessarily reversible because of    the
  255.       equalities like _p_i angles being equal    to -_p_i angles.
  256.  
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))   22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))     MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))
  269.  
  270.  
  271.  
  272.       cartesian_to_cylindrical
  273.  
  274.               ($rho, $theta, $z) = cartesian_to_cylindrical($x,    $y, $z);
  275.  
  276.  
  277.       cartesian_to_spherical
  278.  
  279.               ($rho, $theta, $phi) = cartesian_to_spherical($x,    $y, $z);
  280.  
  281.  
  282.       cylindrical_to_cartesian
  283.  
  284.               ($x, $y, $z) = cylindrical_to_cartesian($rho, $theta, $z);
  285.  
  286.  
  287.       cylindrical_to_spherical
  288.  
  289.               ($rho_s, $theta, $phi) = cylindrical_to_spherical($rho_c,    $theta,    $z);
  290.  
  291.           Notice that when $z is not 0 $rho_s is not equal to
  292.           $rho_c.
  293.  
  294.       spherical_to_cartesian
  295.  
  296.               ($x, $y, $z) = spherical_to_cartesian($rho, $theta, $phi);
  297.  
  298.  
  299.       spherical_to_cylindrical
  300.  
  301.               ($rho_c, $theta, $z) = spherical_to_cylindrical($rho_s, $theta, $phi);
  302.  
  303.           Notice that when $z is not 0 $rho_c is not equal to
  304.           $rho_s.
  305.  
  306.      GGGGRRRREEEEAAAATTTT CCCCIIIIRRRRCCCCLLLLEEEE DDDDIIIISSSSTTTTAAAANNNNCCCCEEEESSSS
  307.       You can compute spherical distances, called ggggrrrreeeeaaaatttt cccciiiirrrrcccclllleeee
  308.       ddddiiiissssttttaaaannnncccceeeessss, by    importing the great_circle_distance function:
  309.  
  310.           use Math::Trig 'great_circle_distance'
  311.  
  312.           $distance    = great_circle_distance($theta0, $phi0,    $theta1, $phi, [, $rho]);
  313.  
  314.       The _g_r_e_a_t _c_i_r_c_l_e _d_i_s_t_a_n_c_e is the shortest distance between
  315.       two points on    a sphere.  The distance    is in $rho units.  The
  316.       $rho is optional, it defaults    to 1 (the unit sphere),
  317.       therefore the    distance defaults to radians.
  318.  
  319.      EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  320.       To calculate the distance between London (51.3N 0.5W)    and
  321.       Tokyo    (35.7N 139.8E) in kilometers:
  322.  
  323.           use Math::Trig qw(great_circle_distance deg2rad);
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))   22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222))))     MMMMaaaatttthhhh::::::::TTTTrrrriiiigggg((((3333))))
  335.  
  336.  
  337.  
  338.           # Notice the 90 - latitude: phi zero is at the North Pole.
  339.           @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
  340.           @T = (deg2rad(139.8),deg2rad(90 - 35.7));
  341.  
  342.           $km =    great_circle_distance(@L, @T, 6378);
  343.  
  344.       The answer may be off    by up to 0.3% because of the irregular
  345.       (slightly aspherical)    form of    the Earth.
  346.  
  347.      BBBBUUUUGGGGSSSS
  348.       Saying use Math::Trig; exports many mathematical routines in
  349.       the caller environment and even overrides some (sin, cos).
  350.       This is construed as a feature by the    Authors, actually...
  351.       ;-)
  352.  
  353.       The code is not optimized for    speed, especially because we
  354.       use Math::Complex and    thus go    quite near complex numbers
  355.       while    doing the computations even when the arguments are
  356.       not. This, however, cannot be    completely avoided if we want
  357.       things like asin(2) to give an answer    instead    of giving a
  358.       fatal    runtime    error.
  359.  
  360.      AAAAUUUUTTTTHHHHOOOORRRRSSSS
  361.       Jarkko Hietaniemi <_j_h_i@_i_k_i._f_i> and Raphael Manfredi
  362.       <_R_a_p_h_a_e_l__M_a_n_f_r_e_d_i@_g_r_e_n_o_b_l_e._h_p._c_o_m>.
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.      Page 6                        (printed 10/23/98)
  394.  
  395.  
  396.  
  397.